/*
The MIT License (MIT)

Copyright (c) 2013 Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdGfxPlatform.h"
#include "hhdIGfxDevice.h"
#include "hhdColor.h"
#include "hhdD3D11.h"
#include "hhdGfxTexture2d.h"
#include "hhdGfxRenderTarget.h"
#include "hhdIGfxViewport.h"

struct ID3D11Device;
struct ID3D11DeviceContext;
struct IDXGIAdapter;
struct IDXGIFactory;
struct IDXGISwapChain;
struct DXGI_SWAP_CHAIN_DESC;

namespace hhd
{
	class GfxRenderTargetD3D11;

	class GfxDeviceD3D11 : public IGfxDevice
	{
	public:
		GfxDeviceD3D11();
		~GfxDeviceD3D11();
		
		void initialize();
		void finalize();

		//create/destroy
		void createViewport(GfxViewport*& out_viewport, const GfxViewportConfig& in_config);
		void destroyViewport(GfxViewport*& inout_viewport);

		void createRenderTarget(const GfxTexture2dConfig& in_config, GfxRenderTargetD3D11*& out_render_target) const;
		void createRenderTargetFromTexture(const GfxTexture2dConfig& in_config, GfxRenderTargetD3D11*& out_render_target, ID3D11Texture2D* in_texture, bool in_is_backbuffer = false) const;
		void destroyRenderTarget(GfxRenderTargetD3D11*& inout_render_target) const;

		void createSwapChain(IDXGISwapChain*& out_swap_chain, DXGI_SWAP_CHAIN_DESC& in_swap_chain_description) const;
		void destroySwapChain(IDXGISwapChain*& inout_swap_chain) const;

		//commands
		void clearRenderTarget(GfxRenderTargetD3D11* in_to_clear, Color in_to_write);
		void applyRenderTargetSetAsDestination(const GfxRenderTargetSet& in_render_target_set);
		void applyRenderTargetSetAsSource(const GfxRenderTargetSet& in_render_target_set);
		void clearRenderTargetSetAsDestination(uint32_t in_count);
		void clearRenderTargetSetAsSource(uint32_t in_count);

	private:
		HHD_NO_CLASS_COPY(GfxDeviceD3D11);

		IDXGIFactory* _dxgi_factory;
		ID3D11Device* _d3d11_device;
		ID3D11DeviceContext* _d3d11_device_context;
	};
}